<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>1-模拟京东快递单号查询案例</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }
        
        .search {
            position: relative;
            width: 178px;
            margin: 100px;
        }
        
        .con {
            display: none;
            position: absolute;
            top: -40px;
            width: 171px;
            border: 1px solid rgba(0, 0, 0, .2);
            box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
            padding: 5px 0;
            font-size: 18px;
            line-height: 20px;
            color: #333;
        }
        
        .con::before {
            content: '';
            width: 0;
            height: 0;
            position: absolute;
            top: 28px;
            left: 18px;
            border: 8px solid #000;
            border-style: solid dashed dashed;
            border-color: #fff transparent transparent;
        }
    </style>
</head>

<body>
    <div class="search">
        <div class="con"></div>
        <label for="jsd">快递单号:<input type="text" placeholder="请输入您的快递单号" class="jd" id="jsd"></label>
    </div>
	<script type="text/javascript">
		//为啥监听，可以对一个元素对象设置多个事件类型
		//获取隐藏的div
		var odiv = document.querySelector('.con');
		var oinput = document.querySelector('input');
		//按键之后抬起
		oinput.addEventListener('keyup',function(e){
			console.log('鼠标按下抬起...');
			//判断input里面是否输入值
			//trim()去除左右空格
			var val = this.value.trim();
			console.log(val);
			console.log(val.length);
			if(val.length>0){
				//显示div
				odiv.style.display = 'block';
				//让输入的内容同步到div中
				odiv.innerHTML = val;
			}else{
				//隐藏div
				odiv.style.display = 'none';
			}
		});
		//input获取焦点
		oinput.addEventListener('focus',function(e){
			console.log('获取焦点');
			//需要获取input的值
			var val = this.value.trim();
			if(val.length>0){
				odiv.style.display = 'block';
				odiv.innerHTML = val;
			}else{
				odiv.style.display = 'none';
			}
		});
		//input失去焦点
		oinput.addEventListener('blur',function(e){
			console.log('失去焦点');
			odiv.style.display = 'none';
		})
		
	</script>
</body>
</html>